package com.example.springboot.config.amqp.rabbitmq.topicrabbitmq;



import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/5/14 14:11
 */
@Configuration
public class TopicRabbitmqConfig {
    public static final String MAN = "topic.man";
    public static final String WOMAN = "topic.woman";

    /**
     * 创建一个队列
     *
     * @return
     */
    @Bean
    public Queue firstQueue() {
        return new Queue(MAN);
    }

    /**
     * @return
     */
    @Bean
    public Queue secondQueue() {
        return new Queue(WOMAN);
    }

    /**
     * 交换机
     *
     * @return
     */
    @Bean
    public Exchange exchange() {
        return new TopicExchange("topicExchange");
    }

    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
    @Bean
    public Binding bindingExchangeMessage() {
        BindingBuilder.GenericArgumentsConfigurer with = BindingBuilder.bind(firstQueue()).to(exchange()).with(MAN);
        return with.noargs();
    }

    // 将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
    @Bean
    public Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#").noargs();
    }


}
